<?php echo $this->element('user');?>
<script type="text/javascript">
$(document).ready(function()
{

	$("#urlChange").change(function() {
		
		id  = $("#urlChange").val();
		document.location = "/assessments/index/"+id;

	});
	<?php
	foreach ($assessments as $assessment)
	{
	?>
	$('#sortable_questiongroups_<?php echo $assessment['Assessment']['id']?>').sortable({

	   stop: function(event, ui) {		   	
			var questiongroup_order_array = $( "#sortable_questiongroups_<?php echo $assessment['Assessment']['id']?>" ).sortable("toArray");
			$.post("/questiongroups/reorder/", { 'data[questiongroups]': [questiongroup_order_array] });
	   }
	});
	
	$("#anc_<?php echo $assessment['Assessment']['id']?>").click(function() 
	{
		var elem = $('#anc_to_<?php echo $assessment['Assessment']['id']?>');
		
		if(elem) 
		{
			$.scrollTo(elem.offset().top-25, 0);
		}
	});
	
	<?php
	
	}
	?>
	$(function() {
		$("#add_assessment_button").click(function() 
		{
			var options = {};		
			$("#create_assessment").toggle('drop',options,500);
			return false;
		});

	});
	
	
});

</script>
<select id="urlChange" label="Choose Course">
<option value=''>View All</option>
	<?php
		foreach ($courseSelect as $course)
		{
			if($course['Course']['id'] == $cid)
			{
				echo "<option value='" . $course['Course']['id']. "' selected='selected'>" . $course['Course']['name'] . "</option>";
			}
			else
			{
				echo "<option value='" .$course['Course']['id']. "'>" . $course['Course']['name'] . "</option>";
			}
		
		}
	?>
</select>
<div id="create_assessment" style="display: none">
	
</div>



<div id="assessments">
<?php 
	foreach ($assessments as $assessment)
	{
		echo $this->element("assessment_css");

		$title = $assessment['Assessment']['name'];
		if(strlen($title) > 80)
			$title = substr($title, 0, 80);
?>
		<div id='anc_<?php echo $assessment['Assessment']['id']?>' class="aTitle">
			<h2><?php echo $title ?></h2>
			<span class="expander"><a href="#" class="contract">-</a></span>
		</div>
		<div class="assessment">
			<?php echo "<div id='edit_div_" . $assessment['Assessment']['id'] . "'>"; ?>
			<span id='anc_to_<?php echo $assessment['Assessment']['id']?>'></span>
			<div class="aToolbar">
				<span class="aToolbarButton">
					<span class="iconNew"></span>
					<?php
						echo $ajax->link(
							'Add A Question Group',
							array( 'controller' => 'questiongroups', 'action' => 'ajax_add', $assessment['Assessment']['id'] ),
							array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading')
						);
					?>
				</span>
				<span class="aToolbarButton">
					<span class="iconEdit"></span>
					<?php 
						echo $ajax->link
						(
							'Edit Assessment',
							array( 'controller' => 'assessments', 'action' => 'edit', $assessment['Assessment']['id'] ),
							array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading')
						);
					?>
				</span>
				<span class="aToolbarButton">
					<span class="iconDelete"></span>
					<?php 
						echo '<a href="/assessments/delete/'.$assessment['Assessment']['id'].'" onClick="return confirm(\'Are you sure?\');">Delete Assessment</a>';
					?>
				</span>
				<span class="aToolbarButton">
					<span class="iconDuplicate"></span>
					<?php 
						echo '<a href="/assessments/duplicate/'.$assessment['Assessment']['id'].'">Duplicate Assessment</a>';
					?>
				</span>
			</div>
			<div class="aTable">
				<div class="aTableHeader">
					<span class="aTableDelete">Delete</span>
					<span class="aTableDuplicate">Duplicate</span>
					<span class="aTableEdit">Edit</span>
					<span class="aTableTitle">Question Group Title</span>
					<span class="aTableQuestion">Question</span>
				</div>
				<?php
				$questiongroups = '';
				$i = 0;//switch between 1 and 0 to toggle between the two list classes
				$anchorCounter = -1;
				foreach ($assessment['Questiongroup'] as $questiongroup)
				{
					$anchorCounter += 1;
					//if($anchorCounter == 6)
					//	echo "<span id='anc_to_" . $assessment['Assessment']['id'] . "'></span>";
					if (strlen($questiongroup['name']) > 40)
					{
						$questiongroup['name'] = substr($questiongroup['name'],0,40);
						$questiongroup['name'] .= '..';
					}
					$question_get_ajax = $ajax->link(
						$questiongroup['name'],
						array( 'controller' => 'questions', 'action' => 'ajax_get', $questiongroup['id'] ),
						array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading_question_big')
					);

					$questiongroup_delete_ajax = $ajax->link(
						'[x]',
						array( 'controller' => 'questiongroups', 'action' => 'ajax_delete', $questiongroup['id'] ),
						array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'confirm' => 'Are you sure?', 'indicator' => 'loading_questiongroup')
					);

					$questiongroup_delete_ajax_img = str_replace('[x]','<span class="iconDelete"></span>',  $questiongroup_delete_ajax);

					$questiongroup_duplicate_ajax = $ajax->link(
						'[x]',
						array( 'controller' => 'questiongroups', 'action' => 'ajax_duplicate', $questiongroup['id'].'/'.$assessment['Assessment']['id'] ),
						array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading_questiongroup')
					);

					$questiongroup_duplicate_ajax_img = str_replace('[x]','<span class="iconDuplicate"></span>',  $questiongroup_duplicate_ajax);
					
					$questiongroup_edit_ajax = $ajax->link(
						'[x]',
						array( 'controller' => 'questiongroups', 'action' => 'ajax_edit', $questiongroup['id'] ),
						array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading_edit')
					);

					$questiongroup_edit_ajax_img = str_replace('[x]','<span class="iconEdit"></span>',  $questiongroup_edit_ajax);
					
					
					$questiongroup_questions = $ajax->link(
						'[x]',
						array( 'controller' => 'questions', 'action' => 'ajax_get', $questiongroup['id'] ),
						array( 'update' => 'edit_div_'.$assessment['Assessment']['id'], 'indicator' => 'loading_edit')
					);

					$questiongroup_questions_img = str_replace('[x]','<span class="iconQuestion"></span>',  $questiongroup_questions);

					if($i == 0)
					{
						$questiongroups .=  '<div class="aTableRowA" id="'.$questiongroup['id'].'">';
						$i = 1;
					}
					else
					{
						$questiongroups .=  '<div class="aTableRowB" id="'.$questiongroup['id'].'">';
						$i = 0;
					}
					$questiongroups .=  
					'
						
						<li>
							<span class="aTableDelete">'.$questiongroup_delete_ajax_img.'</span>
							<span class="aTableDuplicate">'.$questiongroup_duplicate_ajax_img.'</span>
							<span class="aTableEdit">'.$questiongroup_edit_ajax_img.'</span>
							<span class="aTableTitle">'.$questiongroup['name'].'</span>
							<span class="aTableQuestion">'.$questiongroup_questions_img.'</span>
						</li>
						</div>
					';
				}
				echo '<ul id="sortable_questiongroups_'.$assessment['Assessment']['id'].'">
						'.$questiongroups.'
					  </ul>';
				echo '</div>';
				//if($anchorCounter < 6)
					//echo "<span id='anc_to_" . $assessment['Assessment']['id'] . "'></span>";
			?>
			</div>
		</div>
			<?php
			}
				?>
</div>

<span id="add_assessment_button" class="assessmentButton">
		<?php
			$addAssessment = $ajax->link
				(
					'Add Assessment',
					array( 'controller' => 'assessments', 'action' => 'add', ''),
					array( 'update' => 'create_assessment')
				);
		?><?php echo '<span class="iconNew"></span>' . $addAssessment; ?>
</span>

